home *** CD-ROM | disk | FTP | other *** search
- //***********************************************************************
- //
- // FoneList.cpp
- //
- //***********************************************************************
-
- #include <afxwin.h>
- #include <afxext.h>
- #include "Resource.h"
- #include "FoneList.h"
-
- CMyApp myApp;
-
- /////////////////////////////////////////////////////////////////////////
- // CMyApp member functions
-
- BOOL CMyApp::InitInstance ()
- {
- m_pMainWnd = new CMainWindow;
- m_pMainWnd->ShowWindow (m_nCmdShow);
- m_pMainWnd->UpdateWindow ();
- return TRUE;
- }
-
- /////////////////////////////////////////////////////////////////////////
- // CMainWindow message map and member functions
-
- BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
- ON_WM_CREATE ()
- ON_WM_SIZE ()
- ON_COMMAND (IDM_OPTIONS_NEW, OnOptionsNew)
- ON_COMMAND (IDM_OPTIONS_EXIT, OnOptionsExit)
- END_MESSAGE_MAP ()
-
- CMainWindow::CMainWindow ()
- {
- Create (NULL, "Names and Phones", WS_OVERLAPPEDWINDOW, rectDefault,
- NULL, MAKEINTRESOURCE (IDR_MAINFRAME));
- }
-
- int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)
- {
- if (CFrameWnd::OnCreate (lpcs) == -1)
- return -1;
-
- m_ctlListBox.Create (WS_CHILD | WS_VISIBLE, CRect (0, 0, 0, 0),
- this, IDC_LISTBOX);
- return 0;
- }
-
- void CMainWindow::OnSize (UINT nType, int cx, int cy)
- {
- m_ctlListBox.SetWindowPos (NULL, 0, 0, cx, cy,
- SWP_NOMOVE | SWP_NOZORDER);
- }
-
- void CMainWindow::OnOptionsNew ()
- {
- m_ctlListBox.PromptForNewEntry ();
- }
-
- void CMainWindow::OnOptionsExit ()
- {
- SendMessage (WM_CLOSE, 0, 0);
- }
-
- /////////////////////////////////////////////////////////////////////////
- // CEditDialog message map and member functions
-
- BEGIN_MESSAGE_MAP (CEditDialog, CDialog)
- ON_EN_CHANGE (IDC_NAME, OnUpdateOKButton)
- END_MESSAGE_MAP ()
-
- BOOL CEditDialog::OnInitDialog ()
- {
- CDialog::OnInitDialog ();
-
- m_ctlOKButton.AutoLoad (IDOK, this);
- m_ctlCancelButton.AutoLoad (IDCANCEL, this);
- m_ctlPhone.SubclassDlgItem (IDC_PHONE, this);
-
- OnUpdateOKButton ();
- return TRUE;
- }
-
- void CEditDialog::OnUpdateOKButton ()
- {
- CEdit* pCtlName = (CEdit*) GetDlgItem (IDC_NAME);
- m_ctlOKButton.EnableWindow (pCtlName->LineLength ());
- }
-
- void CEditDialog::DoDataExchange (CDataExchange* pDX)
- {
- CDialog::DoDataExchange (pDX);
-
- DDX_Text (pDX, IDC_NAME, m_strName);
- DDV_MaxChars (pDX, m_strName, 32);
- DDX_Text (pDX, IDC_PHONE, m_strPhone);
- DDV_MaxChars (pDX, m_strPhone, 32);
- }
-
- /////////////////////////////////////////////////////////////////////////
- // CFoneListBox message map and member functions
-
- BEGIN_MESSAGE_MAP (CFoneListBox, CListBox)
- ON_WM_CREATE ()
- ON_CONTROL_REFLECT (LBN_DBLCLK, OnEditItem)
- END_MESSAGE_MAP ()
-
- BOOL CFoneListBox::PreCreateWindow (CREATESTRUCT& cs)
- {
- if (!CListBox::PreCreateWindow (cs))
- return FALSE;
-
- cs.style |= LBS_SORT | LBS_USETABSTOPS | LBS_NOTIFY |
- LBS_NOINTEGRALHEIGHT;
- return TRUE;
- }
-
- int CFoneListBox::OnCreate (LPCREATESTRUCT lpcs)
- {
- if (CListBox::OnCreate (lpcs) == -1)
- return -1;
-
- CClientDC dc (this);
- int nHeight = -((dc.GetDeviceCaps (LOGPIXELSY) * 8) / 72);
-
- m_font.CreateFont (nHeight, 0, 0, 0, FW_NORMAL, 0, 0, 0,
- DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
- DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "MS Sans Serif");
- SetFont (&m_font, FALSE);
-
- SetTabStops (128);
- return 0;
- }
-
- void CFoneListBox::OnEditItem ()
- {
- CEditDialog dlg;
-
- CString strItem;
- int nIndex = GetCurSel ();
- GetText (nIndex, strItem);
- int nPos = strItem.Find ('\t');
-
- dlg.m_strName = strItem.Left (nPos);
- dlg.m_strPhone = strItem.Right (strItem.GetLength () - nPos - 1);
-
- if (dlg.DoModal () == IDOK) {
- strItem = dlg.m_strName + "\t" + dlg.m_strPhone;
- DeleteString (nIndex);
- AddString (strItem);
- }
- SetFocus ();
- }
-
- void CFoneListBox::PromptForNewEntry ()
- {
- CEditDialog dlg;
- if (dlg.DoModal () == IDOK) {
- CString strItem = dlg.m_strName + "\t" + dlg.m_strPhone;
- AddString (strItem);
- }
- SetFocus ();
- }
-
- /////////////////////////////////////////////////////////////////////////
- // CNumEdit message map and member functions
-
- BEGIN_MESSAGE_MAP (CNumEdit, CEdit)
- ON_WM_CHAR ()
- END_MESSAGE_MAP ()
-
- void CNumEdit::OnChar (UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- if (((nChar >= '0') && (nChar <= '9')) ||
- (nChar == VK_BACK) || (nChar == '(') || (nChar == ')') ||
- (nChar == '-') || (nChar == ' '))
-
- CEdit::OnChar (nChar, nRepCnt, nFlags);
- }
-